package sweetsaremeat.beehive.Controller;

import sweetsaremeat.beehive.Model.Game;
import sweetsaremeat.beehive.Model.Vector2;
import sweetsaremeat.beehive.View.Camera;
import sweetsaremeat.beehive.View.GameView;
import sweetsaremeat.beehive.View.Hexagon;
import android.graphics.Matrix;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class InputController {
	
	final int NONE = 0;
	final int MOVE = 1;
    final int ZOOM = 2;
    
    final float MOVE_DIFF = 2.0f;
    
    int m_mode = NONE;
    float oldDist = 1f;
    
    Vector2 m_p1Pos = new Vector2();

	
	
	private Game m_game;
	private GameView m_gameView;
	public Camera m_camera;
	
	public InputController()
	{
		m_camera = new Camera();
	}
	
	public void SetGame(Game a_game)
	{
		m_game = a_game;
	}
	
	public void SetView(GameView a_gameView)
	{
		m_gameView = a_gameView;
		
		m_gameView.m_camera = m_camera;
	}
	
	public void HandleMotion(View a_view, MotionEvent a_motion)
	{
		//System.out.println("touched!");
		
		switch(a_motion.getAction())
		{
			case MotionEvent.ACTION_DOWN:
				m_p1Pos.x = a_motion.getRawX();
				m_p1Pos.y = a_motion.getRawY();
				break;
			case MotionEvent.ACTION_MOVE:
				float f_xDiff = m_p1Pos.x - a_motion.getRawX();
				float f_yDiff = m_p1Pos.y - a_motion.getRawY();
				
				if(m_mode == MOVE)
				{
					//move the camera
					m_camera.m_pos.x -= f_xDiff;
					m_camera.m_pos.y -= f_yDiff;
					
					//move the pointer
					m_p1Pos.x = a_motion.getRawX();
					m_p1Pos.y = a_motion.getRawY();
					
					//redraw (since we moved the camera)
					a_view.invalidate();
				}
				else
				{
					
					//check if it has moved
					if(f_xDiff < MOVE_DIFF || f_xDiff > MOVE_DIFF
							|| f_yDiff < MOVE_DIFF || f_yDiff > MOVE_DIFF)
					{
						//set mode to move
						m_mode = MOVE;
						
					}
				}
				break;
			case MotionEvent.ACTION_UP:
				if(m_mode == NONE)
				{
					if(a_motion.getX(0) > m_camera.m_center.x && a_motion.getY(0) < m_camera.m_center.y)
					{
						m_camera.m_scale += 0.1f;
					}
					else if(a_motion.getX(0) < m_camera.m_center.x && a_motion.getY(0) < m_camera.m_center.y)
					{
						m_camera.m_scale -= 0.1f;
					}
					a_view.invalidate();
				}
				m_mode = NONE; //reset the mode
				break;
		/*
			case MotionEvent.ACTION_POINTER_DOWN:
			   oldDist = spacing(a_motion);
			   if (oldDist > 10f) {
			      savedMatrix.set(matrix);
			      midPoint(mid, a_motion);
			      mode = ZOOM;
			   }
			   break;
			   
			case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                break;

			case MotionEvent.ACTION_MOVE:
			   if (mode == NONE) {
			      // ...
			   }
			   else if (mode == ZOOM) {
			      float newDist = spacing(a_motion);
			      if (newDist > 10f) {
			         matrix.set(savedMatrix);
			         float scale = newDist / oldDist;
			         matrix.postScale(scale, scale, mid.x, mid.y);
			      }
			   }
			   break;*/
		}
	}
	
	private float spacing(MotionEvent event) {
		   float x = event.getX(0) - event.getX(1);
		   float y = event.getY(0) - event.getY(1);
		   return FloatMath.sqrt(x * x + y * y);
		}
}
